/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;


import java.util.LinkedList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import modelo.ModeloTablaAlmacen;
import persistencia.AlmacenDAO;
import pojos.Almacen;



/**
 *
 * @author alejandrolopezdelolmo
 */
public class PanelGestionAlmacen extends javax.swing.JPanel {

    
    /**
     * Creates new form PanelSearchBook
     */
    JPanel panelPadre;
    AlmacenDAO administradorAlmacen=new AlmacenDAO();
    List<String> nombreColumnas=new LinkedList();
    
    public PanelGestionAlmacen() {
        initComponents();
        rellenarComboBox();
        
        
           
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPfiltros = new javax.swing.JPanel();
        jChBidAlmacen = new javax.swing.JCheckBox();
        jCBpedidos = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTresultados = new javax.swing.JTable();
        jToolBar = new javax.swing.JToolBar();
        jBnuevoAlmacen = new javax.swing.JButton();
        jBmodificarAlmacen = new javax.swing.JButton();
        jBeliminarAlmacen = new javax.swing.JButton();
        jBbuscar = new javax.swing.JButton();

        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                formFocusGained(evt);
            }
        });

        jPfiltros.setBorder(javax.swing.BorderFactory.createTitledBorder("Filtros de Búsqueda"));
        jPfiltros.setLayout(new java.awt.GridLayout(1, 2));

        jChBidAlmacen.setText("Id Almacen");
        jPfiltros.add(jChBidAlmacen);

        jCBpedidos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPfiltros.add(jCBpedidos);

        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        jTresultados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTresultados);

        jPanel1.add(jScrollPane1);

        jToolBar.setFloatable(false);
        jToolBar.setRollover(true);

        jBnuevoAlmacen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_plus.png"))); // NOI18N
        jBnuevoAlmacen.setToolTipText("Nuevo Almacen");
        jBnuevoAlmacen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBnuevoAlmacenActionPerformed(evt);
            }
        });
        jToolBar.add(jBnuevoAlmacen);

        jBmodificarAlmacen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_edit2.png"))); // NOI18N
        jBmodificarAlmacen.setToolTipText("Modificar Almacen");
        jBmodificarAlmacen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBmodificarAlmacenActionPerformed(evt);
            }
        });
        jToolBar.add(jBmodificarAlmacen);

        jBeliminarAlmacen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_bin.png"))); // NOI18N
        jBeliminarAlmacen.setToolTipText("Eliminar Almacen");
        jBeliminarAlmacen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBeliminarAlmacenActionPerformed(evt);
            }
        });
        jToolBar.add(jBeliminarAlmacen);

        jBbuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_find.png"))); // NOI18N
        jBbuscar.setToolTipText("Realizar Busqueda de Almacenes");
        jBbuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBbuscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 772, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jToolBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(143, 143, 143)
                .addComponent(jPfiltros, javax.swing.GroupLayout.PREFERRED_SIZE, 464, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBbuscar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPfiltros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBbuscar))
                .addGap(79, 79, 79)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jBnuevoAlmacenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBnuevoAlmacenActionPerformed
        JDialogAddAlmacen dialogAddAlmacen=new JDialogAddAlmacen(this,administradorAlmacen);
        dialogAddAlmacen.setVisible(true);
        refrescarComboBox();
        realizarBusqueda();
    }//GEN-LAST:event_jBnuevoAlmacenActionPerformed

    private void jBmodificarAlmacenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBmodificarAlmacenActionPerformed
        modificarAlmacen();
        realizarBusqueda();
    }//GEN-LAST:event_jBmodificarAlmacenActionPerformed

    private void jBbuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBbuscarActionPerformed
        realizarBusqueda();
    }//GEN-LAST:event_jBbuscarActionPerformed

    private void jBeliminarAlmacenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBeliminarAlmacenActionPerformed
         eliminarAlmacen();
         refrescarComboBox();
         realizarBusqueda();
    }//GEN-LAST:event_jBeliminarAlmacenActionPerformed

    private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained
        refrescarComboBox();
    }//GEN-LAST:event_formFocusGained

   
    private void eliminarAlmacen()
    {
        int registroSeleccionado=this.jTresultados.getSelectedRow();
        
        if(registroSeleccionado!=-1)
        {
            int opcion=JOptionPane.showConfirmDialog(this,"¿ESTAS SEGURO QUE DESEAS ELIMINAR ESTE PEDIDO?","ELIMINAR PEDIDO",JOptionPane.INFORMATION_MESSAGE);
                
                if(opcion==JOptionPane.OK_OPTION)
                {
                    Integer codAlmacenEliminar=(Integer)this.jTresultados.getValueAt(registroSeleccionado, 0);
                    Almacen almacenEliminar=this.administradorAlmacen.getAlmacen(codAlmacenEliminar);
                    this.administradorAlmacen.deleteAlmacen(almacenEliminar);
                    JOptionPane.showMessageDialog(this,"Almacen Eliminado Correctamente","Eliminación completada",JOptionPane.INFORMATION_MESSAGE);
                    realizarBusqueda();
                }
             
        }
        else{
            JOptionPane.showMessageDialog(this,"DEBES SELECCIONAR UN ALMACEN DE LA TABLA PARA PODER ELIMINARLO","ERROR AL ELIMINAR",JOptionPane.ERROR_MESSAGE);
        }
     }
     

    private void realizarBusqueda()
    {
        boolean checkIdAlmacen=this.jChBidAlmacen.isSelected();
        Integer idAlmacen=(Integer)this.jCBpedidos.getSelectedItem();
        List<Almacen> listaAlmacenes=new LinkedList();
        nombreColumnas.add("Id Almacen");
        nombreColumnas.add("Descripcion");
        //HA MARCADO EL CHECK BOX DEL ID ALMACEN
        if(checkIdAlmacen)
        {
            listaAlmacenes.add(this.administradorAlmacen.getAlmacen(idAlmacen));
        }
        else
        {
            listaAlmacenes=this.administradorAlmacen.getAlmacenes();
        }
        
        ModeloTablaAlmacen modeloTabla=new ModeloTablaAlmacen(listaAlmacenes,nombreColumnas);
        this.jTresultados.setModel(modeloTabla);
        
    }
    
    private void rellenarComboBox()
    {
        List<Integer> listaCodigos=this.administradorAlmacen.getCodigosAlmacen();
        
        DefaultComboBoxModel modeloCodPedidos=new DefaultComboBoxModel();
        
        for (int i = 0; i < listaCodigos.size(); i++) {
            modeloCodPedidos.addElement(listaCodigos.get(i));
            
        }
        
        this.jCBpedidos.setModel(modeloCodPedidos);
    }
    
    private void modificarAlmacen()
    {
        int registroSeleccionado=this.jTresultados.getSelectedRow();
        
        if(registroSeleccionado!=-1)
        {
                 //COGEMOS EL CODIGO DEL ALMACEN A RECEPCIONAR
                 Integer codAlmacenModificar=(Integer)this.jTresultados.getValueAt(registroSeleccionado, 0);
                 Almacen almacenModificar=this.administradorAlmacen.getAlmacen(codAlmacenModificar);
                 JDialogUpdateAlmacen jdUpdateAlmacen=new JDialogUpdateAlmacen(this,administradorAlmacen,almacenModificar);
                 jdUpdateAlmacen.setVisible(true);
                 realizarBusqueda();
            
        }
        else{
            JOptionPane.showMessageDialog(this,"DEBES SELECCIONAR UN Almacen DE LA TABLA PARA PODER MODIFICARLO","ERROR AL MODIFICAR",JOptionPane.ERROR_MESSAGE);
        }
    }
    
      private void refrescarComboBox()
    {
        this.jCBpedidos.removeAllItems();
        rellenarComboBox();
    }
    
    
     
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBbuscar;
    private javax.swing.JButton jBeliminarAlmacen;
    private javax.swing.JButton jBmodificarAlmacen;
    private javax.swing.JButton jBnuevoAlmacen;
    private javax.swing.JComboBox jCBpedidos;
    private javax.swing.JCheckBox jChBidAlmacen;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPfiltros;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar;
    public javax.swing.JTable jTresultados;
    // End of variables declaration//GEN-END:variables
}
